package Model
{
	import flash.events.Event;
	
	import mx.events.PropertyChangeEvent;
	
	import Controller.AreasController;
	import Controller.TransparentComponentController;

	public class TransparentComponent extends SimpleComponent
	{
		private var _type:String;
		private var _parentComponentId:String;
		private var _length:Number;
		private var _height:Number;
		private var _estimation:Number;
		private var _area:Number;
		private var _construction:Construction;
		private var areasController:AreasController;
		private var transparentController:TransparentComponentController;
		private var propertyChangedEvent:PropertyChangeEvent = new PropertyChangeEvent(PropertyChangeEvent.PROPERTY_CHANGE);
		
		public static var windowCounter:Number = 1;
		public static var doorCounter:Number = 1;
		
		public function TransparentComponent(type:String, description:String="", quantity:Number=1, parentComponentId:String = "", length:Number = 0, height:Number = 0, estimation:Number = 0, area:Number = 0)
		{
			super(description, quantity);
			this._type = type;
			this.id = generateId();
			this._parentComponentId = parentComponentId;
			this._length = length;
			this._height = height;
			this._estimation = estimation;
			this._area = area;
			this.areasController = new AreasController();
			this.transparentController = new TransparentComponentController();
			this.addEventListener("internalChange", handleInternalChange);
			this.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, handlePropertyChangedEvent);
		}
		
		override public function generateId():String
		{
			var result:String;
			
			if(type == "window")
			{
				if(windowCounter <= 9)
				{	
					result = "WI0" + windowCounter.toString();
				}
				else
				{
					result = "WI" + windowCounter.toString();
				}
				
				windowCounter ++;
			}
			else if(type == "door")
			{
				if(doorCounter <= 9)
				{	
					result = "DO0" + doorCounter.toString();
				}
				else
				{
					result = "DO" + doorCounter.toString();
				}
				
				doorCounter++;
			}
			
			return result;
		}
		
		public function get type():String
		{
			return _type;
		}

		public function set type(value:String):void
		{
			_type = value;
		}

		public function get estimation():Number
		{
			return _estimation;
		}

		public function set estimation(value:Number):void
		{
			_estimation = value;
			dispatchEvent(this.internalChangeEvent);
		}

		protected function handlePropertyChangedEvent(event:PropertyChangeEvent):void
		{
			if(this.id.charAt(0) == "W")
			{
				this.areasController.updateWindowAreaField(this);
			}
			else if(this.id.charAt(0) == "D")
			{
				this.areasController.updateDoorAreaField(this);
			}
			
			if(event.property == this.construction)
			{
				this.transparentController.updateTransparentComponentsStatus();
			}
		}
		
		public function get parentComponent():String
		{
			return _parentComponentId;
		}
		
		public function set parentComponent(value:String):void
		{
			_parentComponentId = value;
		}

		public function get length():Number
		{
			return _length;
		}

		public function set length(value:Number):void
		{
			_length = value;
			dispatchEvent(this.internalChangeEvent);
		}

		public function get height():Number
		{
			return _height;
		}

		public function set height(value:Number):void
		{
			_height = value;
			dispatchEvent(this.internalChangeEvent);
		}

		public function get area():Number
		{
			return _area;
		}

		public function set area(value:Number):void
		{
			_area = value;
			this.propertyChangedEvent.property = area;
			dispatchEvent(this.propertyChangedEvent);
		}
		
		override protected function calculateArea():void
		{
			this.area = (this.quantity *(this.length * this.height)) + this.estimation;
		}

		public function get construction():Construction
		{
			return _construction;
		}

		public function set construction(value:Construction):void
		{
			_construction = value;
			this.propertyChangedEvent.property = construction;
			dispatchEvent(this.propertyChangedEvent);
		}
		
		private function handleInternalChange(event:Event):void
		{
			calculateArea();
			this.areasController.updateAreaOverviewStatus();
		}
	}
}